package cnm.validator;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class PatternFactory {

    private static String PATTERN_MAP_FILE = System.getProperty("user.dir") + "/metadata/pattern/pattern.xml";
    private HashMap<String,String> patternMap;
    private HashMap<String,String> classMap;

    private static PatternFactory instance;
    
    private PatternFactory() {
        patternMap = new HashMap<String, String>();
        classMap = new HashMap<String, String>();
        SAXReader reader = new SAXReader();
        try {
        	System.out.println(PATTERN_MAP_FILE);
            Document document = reader.read(new File(PATTERN_MAP_FILE));
            List<Node> list = document.selectNodes("//patternmap/cli");
            for (Node node : list) {
                patternMap.put(
                        node.selectSingleNode("cmd").getText(),
                        System.getProperty("user.dir") + "/metadata/pattern" + node.selectSingleNode("pattern").getText());
                classMap.put(
                        node.selectSingleNode("cmd").getText(),
                        node.selectSingleNode("class").getText());
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } 
    }
    
    public static PatternFactory getInstance() {
        if (instance == null) {
            instance = new PatternFactory();
        }
        return instance;
    }

    public HashMap<String, String> getPatternMap() {
        return patternMap;
    }
    
    public HashMap<String, String> getClassMap() {
        return classMap;
    }
}
